use std::collections::HashMap;
use std::fs;


pub fn converter(input: &str, tp: &str) -> String {
    let s2t_map = serde_json::from_str::<HashMap<String, String>>(&fs::read_to_string("../../s2t.json").unwrap()).unwrap();
    let t2s_map = serde_json::from_str::<HashMap<String, String>>(&fs::read_to_string("../../t2s.json").unwrap()).unwrap();
    let s2t_phrases = serde_json::from_str::<HashMap<String, String>>(&fs::read_to_string("../../STPhrases.json").unwrap()).unwrap();
    let t2s_phrases = serde_json::from_str::<HashMap<String, String>>(&fs::read_to_string("../../TSPhrases.json").unwrap()).unwrap();

    

    let mut result = String::new();
    if tp == "s2t" {
        if let Some(t) = s2t_phrases.get(input) {
            result.push_str(t);
        } else {
            for c in input.chars() {
                if let Some(t) = s2t_map.get(&c.to_string()) {
                    result.push_str(t);
                } else {
                    result.push(c);
                }
            }
        }
    } else if tp == "t2s" {
        if let Some(s) = t2s_phrases.get(input) {
            result.push_str(s);
        } else {
            for c in input.chars() {
                if let Some(s) = t2s_map.get(&c.to_string()) {
                    result.push_str(s);
                } else {
                    result.push(c);
                }
            }
        }
    }
    println!("{}->{}: {}", input, tp, result);
    result
}
